import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:gourmet/route/index.dart';
import 'package:gourmet/ui/scroll_refresh_config.dart';
import 'package:gourmet/utils/colors.dart';
import 'package:gourmet/utils/styles.dart';
import 'package:gourmet/utils/svg.dart';
import 'package:gourmet/utils/tools.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class DeliciousFoodController extends GetxController {
  RefreshController refreshController = RefreshController();

  RxList<String> pageList = RxList([]);

  Future<void> onRefresh() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));
    refreshController.refreshCompleted(resetFooterState: true);
  }

  Future<void> onLoading() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));

    /// if failed, use loadFailed(), if no data return, use LoadNodata()
    if (pageList.length > 15) {
      return refreshController.loadNoData();
    }
    pageList.add('华莱士-凤凰广场2店');
    pageList.add('麦当劳-凤凰广场店');
    pageList.add('华莱士-凤凰广场1店');

    /// 添加
    refreshController.loadComplete();
  }

  @override
  void onInit() {
    super.onInit();
    pageList.add('华莱士-凤凰广场2店');
    pageList.add('麦当劳-凤凰广场店');
    pageList.add('华莱士-凤凰广场1店');
    pageList.add('华莱士-凤凰广场1店');
    pageList.add('顺德佬-人民北路店');
    pageList.add('龙凤茶楼-凤凰广场');
  }

  @override
  void onClose() {
    super.onClose();
    refreshController.dispose();
    Get.delete();
  }
}

class DeliciousFoodPage extends GetView<DeliciousFoodController> {
  const DeliciousFoodPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ColorsUtils.parseColorFromHexString(ColorsUtils.colorF7f7),
      appBar: AppBar(
        backgroundColor: Colors.white,
        leading: IconButton(
            onPressed: Get.back,
            icon: Icon(
              Icons.arrow_back_ios,
              color: ColorsUtils.parseColorFromHexString(ColorsUtils.color3333),
              size: 18,
            )),
        systemOverlayStyle: SystemUiOverlayStyle.dark,
        title: Text(
          '美食HOT',
          style: StylesUtils.customTextStyle(
              color: ColorsUtils.color0000,
              fontSize: StylesUtils.fontSize16,
              fontWeight: FontWeight.w700),
        ),
        actions: [
          IconButton(
            onPressed: () {},
            icon: SvgUtils.assetName('u1115', width: 21, height: 22),
          )
        ],
      ),
      body: Obx(() => scrollRefreshConfig(context)),
    );
  }

  Widget scrollRefreshConfig(BuildContext context) {
    return ScrollRefreshConfig(
      refreshController: controller.refreshController,
      onRefresh: controller.onRefresh,
      onLoading: controller.onLoading,
      child: listViewBuilder(context),
    );
  }

  ListView listViewBuilder(BuildContext context) {
    return ListView.builder(
      itemBuilder: (_, index) {
        return GestureDetector(
          onTap: () {
            Get.toNamed(Routes.storeMain);
          },
          child: Container(
            margin: const EdgeInsets.only(top: 15),
            alignment: Alignment.center,
            child: Container(
              width: 345,
              padding: const EdgeInsets.all(10),
              alignment: Alignment.centerLeft,
              decoration:
                  BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(10.r)),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  ClipRRect(
                    borderRadius: BorderRadius.circular(5),
                    child: Image.network(
                      'https://img0.baidu.com/it/u=655977884,3582260165&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
                      width: 75,
                      height: 75,
                      fit: BoxFit.fill,
                    ),
                  ),
                  Expanded(
                      child: Padding(
                    padding: const EdgeInsets.only(left: 10),
                    child: Column(
                      children: [
                        Align(
                          alignment: Alignment.centerLeft,
                          child: Text(
                            controller.pageList[index],
                            style: StylesUtils.customTextStyle(
                                color: ColorsUtils.color0000,
                                fontSize: StylesUtils.fontSize16,
                                fontWeight: FontWeight.w700),
                          ),
                        ),
                        Padding(
                          padding: const EdgeInsets.symmetric(vertical: 10),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text.rich(TextSpan(children: [
                                TextSpan(
                                    text: '4.5分 ',
                                    style: StylesUtils.customTextStyle(
                                        fontSize: StylesUtils.fontSize12,
                                        color: ColorsUtils.colorFf66)),
                                TextSpan(
                                    text: ' 月售312  人均￥45',
                                    style: StylesUtils.customTextStyle(
                                        color: ColorsUtils.color7979,
                                        fontSize: StylesUtils.fontSize12))
                              ])),
                              Text(
                                '平台转送',
                                style: StylesUtils.customTextStyle(
                                    fontSize: StylesUtils.fontSize12, color: ColorsUtils.colorFf64),
                              )
                            ],
                          ),
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(
                              '起送￥20  免配送费',
                              style: StylesUtils.customTextStyle(
                                  color: ColorsUtils.color7979, fontSize: StylesUtils.fontSize12),
                            ),
                            Text(
                              '20分钟  2.2km',
                              style: StylesUtils.customTextStyle(
                                  color: ColorsUtils.color6666, fontSize: StylesUtils.fontSize12),
                            )
                          ],
                        ),
                        Container(
                          padding: const EdgeInsets.symmetric(vertical: 10),
                          alignment: Alignment.centerLeft,
                          child: Wrap(
                            spacing: 10,
                            alignment: WrapAlignment.start,
                            crossAxisAlignment: WrapCrossAlignment.center,
                            children: [
                              Builder(builder: (_) {
                                final style = StylesUtils.customTextStyle(
                                    color: ColorsUtils.colorFf64, fontSize: StylesUtils.fontSize12);
                                const value = '50减3';
                                double width =
                                    ToolsUtils.calculateTextWidth(value: value, style: style);
                                return Container(
                                  constraints: BoxConstraints.expand(width: width + 20, height: 20),
                                  alignment: Alignment.center,
                                  decoration: BoxDecoration(
                                      borderRadius: BorderRadius.circular(2.r),
                                      border: Border.all(
                                          color: ColorsUtils.parseColorFromHexString(
                                              ColorsUtils.colorFf64))),
                                  child: Text(
                                    value,
                                    style: style,
                                  ),
                                );
                              }),
                              Builder(builder: (_) {
                                final style = StylesUtils.customTextStyle(
                                    color: ColorsUtils.colorFf64, fontSize: StylesUtils.fontSize12);
                                const value = '100减6';
                                double width =
                                    ToolsUtils.calculateTextWidth(value: value, style: style);
                                return Container(
                                  constraints: BoxConstraints.expand(width: width + 20, height: 20),
                                  alignment: Alignment.center,
                                  decoration: BoxDecoration(
                                      borderRadius: BorderRadius.circular(2.r),
                                      border: Border.all(
                                          color: ColorsUtils.parseColorFromHexString(
                                              ColorsUtils.colorFf64))),
                                  child: Text(
                                    value,
                                    style: style,
                                  ),
                                );
                              }),
                              Builder(builder: (_) {
                                final style = StylesUtils.customTextStyle(
                                    color: ColorsUtils.colorFf64, fontSize: StylesUtils.fontSize12);
                                const value = '150减10';
                                double width =
                                    ToolsUtils.calculateTextWidth(value: value, style: style);
                                return Container(
                                  constraints: BoxConstraints.expand(width: width + 10, height: 20),
                                  alignment: Alignment.center,
                                  decoration: BoxDecoration(
                                      borderRadius: BorderRadius.circular(2.r),
                                      border: Border.all(
                                          color: ColorsUtils.parseColorFromHexString(
                                              ColorsUtils.colorFf64))),
                                  child: Text(
                                    value,
                                    style: style,
                                  ),
                                );
                              })
                            ],
                          ),
                        ),
                        Align(
                          alignment: Alignment.centerRight,
                          child: Wrap(
                            alignment: WrapAlignment.end,
                            crossAxisAlignment: WrapCrossAlignment.center,
                            spacing: 10,
                            children: [
                              SizedBox(
                                width: 70,
                                child: Column(
                                  children: [
                                    Image.network(
                                      'https://img2.baidu.com/it/u=1382898662,2005223166&fm=253&fmt=auto&app=138&f=JPEG?w=658&h=411',
                                      width: 70,
                                      height: 60,
                                      fit: BoxFit.fill,
                                    ),
                                    Container(
                                      margin: const EdgeInsets.symmetric(vertical: 5),
                                      alignment: Alignment.centerLeft,
                                      child: Text(
                                        '香辣鸡翅',
                                        style: StylesUtils.customTextStyle(
                                            fontSize: StylesUtils.fontSize12,
                                            color: ColorsUtils.color0000),
                                        maxLines: 1,
                                        overflow: TextOverflow.ellipsis,
                                      ),
                                    ),
                                    Align(
                                      alignment: Alignment.centerLeft,
                                      child: Text(
                                        '￥20',
                                        style: StylesUtils.customTextStyle(
                                            fontSize: StylesUtils.fontSize12,
                                            color: ColorsUtils.colorFf64),
                                      ),
                                    )
                                  ],
                                ),
                              ),
                              SizedBox(
                                width: 70,
                                child: Column(
                                  children: [
                                    Image.network(
                                      'https://img0.baidu.com/it/u=4046734346,2343640913&fm=253&fmt=auto&app=138&f=JPEG?w=658&h=411',
                                      width: 70,
                                      height: 60,
                                      fit: BoxFit.fill,
                                    ),
                                    Container(
                                      margin: const EdgeInsets.symmetric(vertical: 5),
                                      alignment: Alignment.centerLeft,
                                      child: Text(
                                        '百事可乐（全国免费品尝）',
                                        style: StylesUtils.customTextStyle(
                                            fontSize: StylesUtils.fontSize12,
                                            color: ColorsUtils.color0000),
                                        maxLines: 1,
                                        overflow: TextOverflow.ellipsis,
                                      ),
                                    ),
                                    Align(
                                      alignment: Alignment.centerLeft,
                                      child: Text.rich(TextSpan(children: [
                                        TextSpan(
                                            text: '￥8',
                                            style: StylesUtils.customTextStyle(
                                                fontSize: StylesUtils.fontSize12,
                                                color: ColorsUtils.colorFf64)),
                                        TextSpan(
                                            text: ' ￥10',
                                            style: StylesUtils.customTextStyle(
                                                color: ColorsUtils.color9999,
                                                fontSize: StylesUtils.fontSize12,
                                                decoration: TextDecoration.lineThrough,
                                                decorationColor:
                                                    ColorsUtils.parseColorFromHexString(
                                                        ColorsUtils.color9999),
                                                decorationStyle: TextDecorationStyle.solid,
                                                decorationThickness: 1))
                                      ])),
                                    )
                                  ],
                                ),
                              ),
                              SizedBox(
                                width: 70,
                                child: Column(
                                  children: [
                                    Image.network(
                                      'https://img0.baidu.com/it/u=74519030,2219195209&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=281',
                                      width: 70,
                                      height: 60,
                                      fit: BoxFit.fill,
                                    ),
                                    Container(
                                      margin: const EdgeInsets.symmetric(vertical: 5),
                                      alignment: Alignment.centerLeft,
                                      child: Text(
                                        '香芋夹心饼',
                                        style: StylesUtils.customTextStyle(
                                            fontSize: StylesUtils.fontSize12,
                                            color: ColorsUtils.color0000),
                                        maxLines: 1,
                                        overflow: TextOverflow.ellipsis,
                                      ),
                                    ),
                                    Align(
                                      alignment: Alignment.centerLeft,
                                      child: Text(
                                        '￥18',
                                        style: StylesUtils.customTextStyle(
                                            fontSize: StylesUtils.fontSize12,
                                            color: ColorsUtils.colorFf64),
                                      ),
                                    )
                                  ],
                                ),
                              )
                            ],
                          ),
                        )
                      ],
                    ),
                  ))
                ],
              ),
            ),
          ),
        );
      },
      itemCount: controller.pageList.length,
    );
  }
}
